package com.personal.portal.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Experience;
import com.personal.portal.admin.mapper.ExperienceMapper;
import com.personal.portal.admin.service.ExperienceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 工作经历服务实现类
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Service
public class ExperienceServiceImpl implements ExperienceService {

    @Resource
    private ExperienceMapper experienceMapper;

    @Override
    public Experience queryById(Long id) {
        validateId(id);
        return experienceMapper.selectOneById(id);
    }

    @Override
    public Experience queryByEntity(Experience experience) {
        QueryWrapper queryWrapper = buildQueryWrapper(experience);
        return experienceMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public List<Experience> queryList(Experience experience) {
        QueryWrapper queryWrapper = buildQueryWrapper(experience);
        return experienceMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Result.PageResult<Experience> queryPage(int pageNum, int pageSize, Experience experience) {
        QueryWrapper queryWrapper = buildQueryWrapper(experience);
        Page<Experience> paginate = experienceMapper.paginate(pageNum, pageSize, queryWrapper);
        return new Result.PageResult<>(paginate.getRecords(), paginate.getTotalRow(), pageNum, pageSize);
    }

    @Override
    public int insert(Experience experience) {
        validateExperience(experience);
        return experienceMapper.insert(experience);
    }

    @Override
    public int update(Experience experience) {
        if (experience.getId() == null) {
            throw new IllegalArgumentException("工作经历ID不能为空");
        }
        validateId(experience.getId());
        validateExperience(experience);
        
        // 检查是否存在
        Experience existing = experienceMapper.selectOneById(experience.getId());
        if (existing == null) {
            throw new IllegalArgumentException("工作经历信息不存在");
        }
        
        return experienceMapper.update(experience);
    }

    @Override
    public int deleteById(Long id) {
        validateId(id);
        
        // 检查是否存在
        Experience existing = experienceMapper.selectOneById(id);
        if (existing == null) {
            throw new IllegalArgumentException("工作经历信息不存在");
        }
        
        return experienceMapper.deleteById(id);
    }

    @Override
    public List<Long> deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        List<Long> successIds = new ArrayList<>();
        List<Long> failedIds = new ArrayList<>();

        for (Long id : ids) {
            try {
                validateId(id);
                
                // 检查是否存在
                Experience existing = experienceMapper.selectOneById(id);
                if (existing == null) {
                    failedIds.add(id);
                    continue;
                }

                int result = experienceMapper.deleteById(id);
                if (result > 0) {
                    successIds.add(id);
                } else {
                    failedIds.add(id);
                }
            } catch (Exception e) {
                failedIds.add(id);
            }
        }

        if (!successIds.isEmpty()) {
            return successIds;
        } else {
            throw new IllegalArgumentException("批量删除失败");
        }
    }

    /**
     * 验证工作经历信息
     */
    private void validateExperience(Experience experience) {
        if (experience == null) {
            throw new IllegalArgumentException("工作经历信息不能为空");
        }
        if (StringUtils.isBlank(experience.getCompany())) {
            throw new IllegalArgumentException("公司名称不能为空");
        }
        if (StringUtils.isBlank(experience.getPosition())) {
            throw new IllegalArgumentException("职位名称不能为空");
        }
        if (experience.getStartDate() == null) {
            throw new IllegalArgumentException("开始时间不能为空");
        }
    }

    /**
     * 验证ID
     */
    private void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的工作经历ID");
        }
    }

    /**
     * 构建查询条件
     */
    private QueryWrapper buildQueryWrapper(Experience experience) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Experience::getId, experience.getId(), experience.getId() != null);
        queryWrapper.like(Experience::getCompany, experience.getCompany(), StringUtils.isNotEmpty(experience.getCompany()));
        queryWrapper.like(Experience::getPosition, experience.getPosition(), StringUtils.isNotEmpty(experience.getPosition()));
        queryWrapper.like(Experience::getDepartment, experience.getDepartment(), StringUtils.isNotEmpty(experience.getDepartment()));
        queryWrapper.like(Experience::getLevel, experience.getLevel(), StringUtils.isNotEmpty(experience.getLevel()));
        queryWrapper.ge(Experience::getStartDate, experience.getStartDate(), experience.getStartDate() != null);
        queryWrapper.le(Experience::getEndDate, experience.getEndDate(), experience.getEndDate() != null);
        queryWrapper.eq(Experience::getIsCurrent, experience.getIsCurrent(), experience.getIsCurrent() != null);

        queryWrapper.orderBy(Experience::getStartDate, false);
        queryWrapper.orderBy(Experience::getId, true);
        return queryWrapper;
    }
}